const util = require("../../../utils/util")
const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const status_list = getApp().globalData.status_list
const status_bgColors = getApp().globalData.status_bgColors


const today = util.formatDate(new Date())
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status_list,
    status_bgColors,
    default_avatar:'https://thirdwx.qlogo.cn/mmopen/vi_32/ajzumj6ibP0NPaP8GCe8Xm0tNE5cK0xWuE5uYiamJWkCFFfBbncJh86wuyfL7vncIxEpZhmm3soVZ9vU7fejmibpg/132',
    lis:[],
    page_no:0,
    page_size:10,
    no_more:false,
    offsetTop:0,
    act_label:1,
    date_set:'',
    search_content:'',
    phone:'',
    id_no:'',
    is_stu:'',
  },


  // 输入查询内容
  search_content(e){
    let value = e.detail.value
    let search_content = value.split(' ').join('');
    this.setData({
      search_content
    })
  },
  

    // 点击开始搜索
    search_btn(){
      getApp().get_vibration()
      if(!this.data.search_content){
        wx.showToast({
          title: '请输入内容',
          icon: 'error',
        })
        return 
      }
      wx.request({
        url: domainName + '/set_user_init',
        data: {
          search_content:this.data.search_content
        },...parameter,
        success: res => {
          let lis = res.data.lis
          this.setData({
            tatal_count:res.data.tatal_count
          })
          if(lis.length==0){
            wx.showModal({
              content: '没有匹配的用户！',
              showCancel: false,
              title: '提示',
            })
          }else{
            this.setData({
              page_no:0,
              no_more:false,
              lis:lis
            })
          }
        },
      })
    },
  
  

  // 清楚搜索内容
  cancel(){
    getApp().get_vibration()
    this.setData({
      search_content:'',
      page_no:0,
    })
    this.onLoad(this.options)
  },


  add_to_jg(e){
    getApp().get_vibration()
    let index = e.currentTarget.dataset.index
    let stu_id = this.data.lis[index].user_id
    wx.navigateTo({
      url:'/pages/students/stu_register/register_stu/register_stu?stu_id=' + stu_id
    })
  },


  set_realname(e){
    getApp().get_vibration()
    let index = e.currentTarget.dataset.index
    let user_id = this.data.lis[index].user_id
    this.can_onshow = true
    this.can_scroll = true
    this.setData({
      scrollTop:e.detail.y-e.touches[0].clientY,
    })
    wx.navigateTo({
      url: '/pages/users/identification/identification?user_id=' + user_id,
    })
  },


  set_phone(e){
    getApp().get_vibration()
    let index = e.currentTarget.dataset.index
    let user_id = this.data.lis[index].user_id
    let phone = this.data.lis[index].phone || ''
    this.can_onshow = true
    this.can_scroll = true
    this.setData({
      scrollTop:e.detail.y-e.touches[0].clientY,
    })
    wx.navigateTo({
      url: '/pages/users/set_user_phone/set_user_phone?user_id=' + user_id + '&phone=' + phone,
    })
  },


  view_info(e){
    getApp().get_vibration()
    let index = e.currentTarget.dataset.index
    let user_id = this.data.lis[index].user_id
    this.can_onshow = true
    this.can_scroll = true
    this.setData({
      scrollTop:e.detail.y-e.touches[0].clientY,
    })
    wx.navigateTo({
      url: '/pages/users/basic_info/basic_info?user_id=' + user_id,
    })
  },

  view_stuInfo(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    let jg_stu_id = this.data.lis[index].jg_stu_id
    wx.navigateTo({
      url: '/pages/students/stu_register/stuRegister_info/stuRegister_info?jg_stu_id=' + jg_stu_id,
    })
  },

  delete_user(e){
    getApp().get_vibration()
    let index = e.currentTarget.dataset.index
    let user_id = this.data.lis[index].user_id
    let real_name = this.data.lis[index].real_name
    let nick_name = this.data.lis[index].nick_name
    wx.showModal({
      content: '确定要删除【' + (real_name || nick_name) + '】吗？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          this.can_scroll = true
          this.setData({
            scrollTop:e.detail.y-e.touches[0].clientY,
          })
          wx.request({
            url: domainName + '/set_user_delete',
            data: {user_id},...parameter,
            success: res => {
              if(res.data.delete_res){
                wx.showToast({
                  title: '删除完成',
                })

                this.data.page_no -= 1
                this.page_init(0, this.data.lis.length,this.data.search_content,this.data.phone, this.data.id_no,this.data.date_set,this.data.is_stu)
              }
            },
          })
        }
      },
    })


  },


  // 筛选查看
  select_label(e){
    getApp().get_vibration()
    let act_label = e.currentTarget.id
    this.setData({act_label,no_more:false})
    if(act_label == 1){
      this.data.phone = ''
      this.data.id_no = ''
      this.data.date_set = ''
      this.data.is_stu = ''
    }else if(act_label == 2){
      this.data.date_set = today
      this.data.phone = ''
      this.data.id_no = ''
      this.data.is_stu = ''
    }else if(act_label == 3){
      this.data.phone = 'notNULL'
      this.data.id_no = ''
      this.data.date_set = ''
      this.data.is_stu = ''
    }else if(act_label == 4){
      this.data.phone = ''
      this.data.date_set = ''
      this.data.id_no = 'notNULL'
      this.data.is_stu = ''
    }else if(act_label == 5){
      this.data.phone = ''
      this.data.date_set = ''
      this.data.id_no = ''
      this.data.is_stu = true
    }
    this.data.page_no = 0
    this.page_init(this.data.page_no,this.data.page_size,this.data.search_content,this.data.phone,this.data.id_no,this.data.date_set,this.data.is_stu)
  },


  // 搜索
  page_init(page_no,page_size,search_content,phone,id_no,date_set,is_stu){
    wx.showLoading()
    wx.request({
      url: domainName + '/set_user_init',
      data: {page_no, page_size, search_content, phone, id_no, date_set, is_stu},...parameter,
      success: res => {
        wx.hideLoading()
        this.setData({
          tatal_count:res.data.tatal_count
        })
        let lis = res.data.lis
        if(this.data.page_no){  // 避免刷新页面数据时，新lis与原来的lis叠加。
          this.setData({
            lis:this.data.lis.concat(lis),
            page_no:this.data.page_no + 1,
          })
        }else{
          this.setData({
            lis:lis,
            page_no:1,
          })
        }

        if(lis.length==0){
          this.setData({
            no_more:true
          })
        }
        this.can_onshow = 1
        // 页面滚动
        if(this.can_scroll){
          this.can_scroll = false
          wx.pageScrollTo({
            duration:0,
            scrollTop:this.data.scrollTop
          })
        }
      },
    })
  },


  onLoad(options){
    this.page_init(this.data.page_no,this.data.page_size,this.data.search_content,this.data.phone, this.data.id_no,this.data.date_set,this.data.is_stu)
  },

  onShow(){

  },

  onReachBottom(){
    if(!this.data.no_more){
      this.page_init(this.data.page_no,this.data.page_size,this.data.search_content,this.data.phone, this.data.id_no,this.data.date_set,this.data.is_stu)
    }
  },

  onPullDownRefresh(){
    this.can_onshow = false
    this.data.page_no=0
    this.page_init(this.data.page_no,this.data.page_size,this.data.search_content,this.data.phone, this.data.id_no,this.data.date_set,this.data.is_stu)
    wx.stopPullDownRefresh()
  }
  
})